package com.itqn.servlet;

import com.itqn.pojo.Category;
import com.itqn.pojo.Product;
import com.itqn.service.Impl.ProductServiceImpl;
import com.itqn.service.ProductService;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

@WebServlet(value = "/ProductUpdateServlet")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //获取参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String category = request.getParameter("category");
        String price = request.getParameter("price");
        String file = request.getParameter("file");
        String desc = request.getParameter("desc");
        //把属性封装到实体类对象中
        Product product=new Product();
        product.setName(name);
        product.setCategoryId(Long.valueOf(category));
        product.setPrice(new BigDecimal(price));
        product.setImage(file);
        product.setDescription(desc);
        product.setId(Long.parseLong(id));
        //创建业务逻辑层对象
        ProductService productService=new ProductServiceImpl();
        int result = productService.updateGoods(product);
        PrintWriter writer = response.getWriter();
        writer.write(result>0?"ok":"no");
    }
}
